%macro similpendientes (data= , group=, var=  , time=, 
						timeunit=365, movave='NO', lag=12);

/***(se estandarizan los datos)*******************/
PROC STANDARD DATA=&data OUT=DataStd MEAN=0 STD=1;
	BY &group; 
	VAR &var;
RUN;

%IF &movave='NO' %THEN %DO; 
	Title 'Pendientes utilizando datos originales'; 
	%PEND_REG (data=DataStd, group=&group, var=&var, time=&time, timeunit=&timeunit );		
    %END;
%ELSE %IF &movave='SI' %THEN %DO; 
	Title 'Pendientes utilizando medias moviles de longitud &lag'; 
	%MEDIASMOVILES (data=DataStd, group=&group, var=&var, time=&time, timeunit=&timeunit, lag=&lag);		
	%END;

PROC IML; 
	USE PENDIENTES; 
	READ ALL VAR {&var}   INTO mVariables; 
	read all var {&group} into mAgrupacion;
	nGroup = NROW(mVariables);
	nVar   = NCOL(mVariables); 
	mdist_tend       = j(nGroup,nGroup,0); 
	mdist_tend_cua   = j(nGroup,nGroup,0);
	msimil_norm_pend = j(nGroup,nGroup,0);
	msimil_prom_pend = j(nGroup,nGroup,0);
	msimil_chi2_pend = j(nGroup,nGroup,0);
	
	USE DESVIACIONES ;
	read all var {&VAR} into mstderr ;
	media = sqrt(2/3.14159)*nVar; 
	varianza = (1-(2/3.14159))*nVar; 
	desvia = sqrt(varianza);	
	
	DO I=1 TO nGroup; 
		DO J=1 TO nGroup; 
			suma_abs=0; suma_cua=0; probt=0;SUMAT2=0; sumat=0;

			DO K=1 TO nVar; 
				denomi = sqrt(mstderr[i,k]**2 + mstderr[j,k]**2); 
				t = abs(mVariables[i,k]-mVariables[j,k])/denomi;
				sumat = sumat + t;            			/**suma valores ab. estand.**/
				probt = probt + 2*(1-probnorm(t)); 		/**calcu de su prob**/
				sumat2 = sumat2 + t**2;          		/**suma valor ab. estan al cuad**/
			end;

	  		sumaabs_estand = (sumat-MEDIA)/DESVIA;
			cero = -media/desvia;
			numerador = 1-probnorm(sumaabs_estand); 
			denominador = 1-probnorm(cero);
			msimil_norm_pend[i,j] = numerador/denominador;

			/*dist normal**/
		    mdist_tend[i,j] = sumat;          /**suma diferencias abs estan*/
			mdist_tend_cua[i,j] = sumat2;       	  /**dist por suma de cuadrados**/
			msimil_prom_pend[i,j] = probt/nVar;      /**similitud por promedio**/
			msimil_chi2_pend[I,J] = 1-PROBCHI(SUMAT2,nVar);   /**simil por chi2**/
		end;
	end;
	
	U1 = CHAR(mAgrupacion,3);  
	UU1 = J(nGroup,1,'AGR');               
	cAgrupacion = CONCAT(UU1,U1);
	
	create dist_tend from mdist_tend [colname=cAgrupacion rowname=cAgrupacion];
	append from mdist_tend [rowname=cAgrupacion];
	close dist_tend;

	create dist_tend_cuad from mdist_tend_cua [colname=cAgrupacion rowname=cAgrupacion];
	append from mdist_tend_cua [rowname=cAgrupacion];
	close dist_tend_cuad;

	create simil_norm_pend from msimil_norm_pend [colname=cAgrupacion rowname=cAgrupacion];
	append from msimil_norm_pend [rowname=cAgrupacion];
	close simil_norm_pend;

	create simil_prom_pend from msimil_prom_pend [colname=cAgrupacion rowname=cAgrupacion];
	append from msimil_prom_pend [rowname=cAgrupacion];
	close simil_prom_pend;

	create simil_chi2_pend from msimil_chi2_pend [colname=cAgrupacion rowname=cAgrupacion];
	append from msimil_chi2_pend [rowname=cAgrupacion];
	close simil_chi2_pend;

/* Delete working files */
PROC DATASETS library=work ; 
	DELETE DataStd;
RUN;
QUIT;

%mend similpendientes; 
